﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class WorkShopService : IWorkShopService
    {
        private readonly IWorkShopOperate _workShopOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public WorkShopService(IUnitOfWork unitOfWork, IMapper mapper , IWorkShopOperate workShopOperate)
        {
            _mapper = mapper; 
            _unitOfWork = unitOfWork;
            _workShopOperate = workShopOperate;

        }
        public bool AddSingleWorkShop(WorkShop workShop)
        {
            try
            {
                _workShopOperate.AddWorkShop(workShop);
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        public bool DeleteWorkShop(string id)
        {
            try
            {
                WorkShop workShop = _workShopOperate.GetWorkShopById(id);
                if (workShop != null)
                {
                    _workShopOperate.DeleteWorkShop(workShop);
                    _unitOfWork.Commit();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public List<WorkShop> GetAllWorkShop()
        {
            try
            {
                return _workShopOperate.GetAllWorkShopList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public List<WorkShopModel> GetAllWorkShopModel()
        {
            try
            {
                List<WorkShop> workShops = _workShopOperate.GetAllWorkShopList();
                List<WorkShopModel> modelList = new List<WorkShopModel>();
                foreach (WorkShop workShop in workShops)
                {
                    modelList.Add(_mapper.Map<WorkShopModel>(workShop));
                }
                return modelList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool UpdateWorkShop(WorkShop workShop)
        {
            try
            {
                WorkShop entity = _workShopOperate.GetWorkShopById(workShop.WorkShopId);
                if (entity != null)
                {
                    _mapper.Map<WorkShop, WorkShop>(workShop,entity);
                    _workShopOperate.UpdateWorkShop(entity);
                }
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public WorkShopModel GetWorkShopById(string id)
        {
            try
            {
                WorkShop workShop = _workShopOperate.GetWorkShopById(id);
                if (workShop != null)
                {
                    return _mapper.Map<WorkShopModel>(workShop);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool BatchDelWorkShop(List<string> ids)
        {
            try
            {
                List<WorkShop> workShops = _workShopOperate.GetWorkShopByIdList(ids).ToList();
                if (workShops.Count > 0)
                {
                    _workShopOperate.BatchDelWorkShop(workShops);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}
